﻿function blockUI() {
	$.blockUI
	(
		{
			message: "<img style='border: none' src='/Content/Images/ajax-loader.gif' />",
			css: { border: 'none', backgroundColor: 'transparent', color: '#fff' }
		}
	);
}

function unblockUI() {
	$.unblockUI();
}

function setMessage(message) {
    $('.message').html(message);
}

function setMessages(messages) {
    $('.message').html(messages.join("<br/>"));
}

$(document).ready
(
// ajax entry rank
	function () {
	    $("a.image").lightBox();
	    SyntaxHighlighter.all();

	    $("button.rankButton.entryRank").click
		(
			function (e) {
			    e.preventDefault();

			    var rank = $(this).text();
			    if (rank === "+")
			        rank = 1;
			    else
			        rank = -1;

			    var entryId = $(this).parent().find("[name=entryId]").attr("value");
			    entryId = parseInt(entryId);

			    var $rankSpan = $(this).parent().parent().children(".currentRank");

			    $.ajax
				(
					{
					    type: "POST",
					    url: "/Entries/RankAjax",
					    data: { entryId: entryId, value: rank },
					    beforeSend: function () {
					        blockUI();
					    },
					    success: function (result) {
					        $.cookie("EntryRank" + entryId, rank, { path: '/' });

					        if (result.result) {
					            $rankSpan.text(result.currentRank);
					        }
					        setMessage(result.message);
					        unblockUI();
					    },
					    error: function (result) {
					        setMessage(resources.UnexpectedAjaxError);
					        unblockUI();
					    }
					}
				);
			}
		);

	    // ajax comment rank
	    $("button.rankButton.commentRank").live
		(
			"click",
			function (e) {
			    e.preventDefault();

			    var rank = $(this).text();
			    if (rank === "+")
			        rank = 1;
			    else
			        rank = -1;

			    var entryId = $(this).parent().find("[name=entryId]").attr("value");
			    var commentId = $(this).parent().find("[name=commentId]").attr("value");
			    commentId = parseInt(commentId);

			    var $rankSpan = $(this).parent().parent().children(".currentRank");

			    $.ajax
				(
					{
					    type: "POST",
					    url: "/Comments/RankAjax",
					    data: { entryId: entryId, commentId: commentId, value: rank },
					    beforeSend: function () {
					        blockUI();
					    },
					    success: function (result) {
					        $.cookie("CommentRank" + commentId, rank, { path: '/' });
					        if (result.result) {
					            $rankSpan.text(result.currentRank);
					        }
					        setMessage(result.message);
					        unblockUI();
					    },
					    error: function (result) {
					        setMessage(resources.UnexpectedAjaxError);
					        unblockUI();
					    }
					}
				);
			}
		);

	    // ajax comments saving
	    $("button.linkButton.addComment").click
		(
			function (e) {
			    e.preventDefault();
			    $this = $(this);
			    var entryId = $this.siblings("[name=entryId]").val();
			    var $author = $this.siblings(".editCommentHeader").children("input");
			    var $content = $this.siblings(".editCommentContent").children("textarea");
			    var $captchaValue = $this.siblings(".captcha").children("[name=captchaValue]");
			    var $captchaKey = $this.siblings(".captcha").children("[name=captchaKey]");

			    $.ajax
				(
					{
					    type: "POST",
					    url: "/Comments/SaveCommentAjax",
					    data: { entryId: entryId, author: $author.val(), content: $content.val(), captchaValue: $captchaValue.val(), captchaKey: $captchaKey.val() },
					    beforeSend: function () {
					        blockUI();
					    },
					    success: function (result) {
					        $result = $(result);

					        var message = $result.children("div.message").html();
					        setMessage(message);

					        var result = $result.children("div.result").text() == "1";
					        if (result) {
					            var commentView = $result.children("div.comment");
					            $("h2#addComment").before(commentView);

					            var commentsCount = $result.children("div.commentsCount").text();
					            commentsCount = parseInt(commentsCount);

					            $("span.commentsCount").text(commentsCount);

					            $author.val("");
					            $content.val("");
					            $captchaValue.val("");
					        }

					        var isAuthorValid = $result.children("div.isAuthorValid").text() == "1";
					        if (!isAuthorValid)
					            $("div.editCommentHeader input").addClass("errorInput");
					        else
					            $("div.editCommentHeader input").removeClass("errorInput");

					        var isContentValid = $result.children("div.isContentValid").text() == "1";
					        if (!isContentValid)
					            $("div.editCommentContent textarea").addClass("errorInput");
					        else
					            $("div.editCommentContent textarea").removeClass("errorInput");

					        var isCaptchaValid = $result.children("div.isCaptchaValid").text() == "1";
					        if (!isCaptchaValid)
					            $("div.captcha input").addClass("errorInput");
					        else
					            $("div.captcha input").removeClass("errorInput");

					        var captchaA = $result.children("div.captchaValueA").text();
					        $("span.captchaA").text(captchaA);

					        var captchaB = $result.children("div.captchaValueB").text();
					        $("span.captchaB").text(captchaB);

					        var captchaKey = $result.children("div.captchaKey").text();
					        $("input[name=captchaKey]").val(captchaKey);

					        unblockUI();
					    },
					    error: function (result) {
					        setMessage(resources.UnexpectedAjaxError);
					        unblockUI();
					    }
					}
				);
			}
		);

	    $("button.linkButton.sendMessage").click
        (
            function (e) {
                e.preventDefault();

                var $parent = $(this).parent();
                var $sender = $parent.find("[name=sender]");
                var $email = $parent.find("[name=email]");
                var $messageContent = $parent.find("[name=messageContent]");

                var $captchaA = $parent.find(".captchaA");
                var $captchaB = $parent.find(".captchaB");
                var $captchaKey = $parent.find("[name=captchaKey]");
                var $captchaValue = $parent.find("[name=captchaValue]");

                $.ajax
				(
					{
					    type: "POST",
					    url: "/Contact/SendMessageAjax",
					    data: { sender: $sender.val(), email: $email.val(), messageContent: $messageContent.val(), captchaKey: $captchaKey.val(), captchaValue: $captchaValue.val() },
					    beforeSend: function () {
					        blockUI();
					    },
					    success: function (result) {
					        $sender.removeClass("errorInput");
					        $email.removeClass("errorInput");
					        $messageContent.removeClass("errorInput");
					        $captchaValue.removeClass("errorInput");

					        if (result.result) {
					            $sender.val("");
					            $email.val("");
					            $messageContent.val("");
					        }
					        else {
					            if (!result.isSenderValid)
					                $sender.addClass("errorInput");
					            if (!result.isMessageContentValid)
					                $messageContent.addClass("errorInput");
					            if (!result.isCaptchaValid)
					                $captchaValue.addClass("errorInput");
					        }

					        $captchaA.text(result.captchaValueA);
					        $captchaB.text(result.captchaValueB);
					        $captchaKey.val(result.captchaKey);
					        $captchaValue.val("");

					        setMessages(result.messages);
					        unblockUI();
					    },
					    error: function (result) {
					        setMessage(resources.UnexpectedAjaxError);
					        unblockUI();
					    }
					}
				);
            }
        );
	}
);